package com.cuhk.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cuhk.common.valid.AddGroup;
import com.cuhk.common.valid.UpdateGroup;
import com.cuhk.gulimall.product.entity.AttrEntity;
import com.cuhk.gulimall.product.service.AttrAttrgroupRelationService;
import com.cuhk.gulimall.product.service.AttrService;
import com.cuhk.gulimall.product.service.CategoryService;
import com.cuhk.gulimall.product.service.impl.AttrAttrgroupRelationServiceImpl;
import com.cuhk.gulimall.product.vo.AttrGroupRelationVo;
import com.cuhk.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cuhk.gulimall.product.entity.AttrGroupEntity;
import com.cuhk.gulimall.product.service.AttrGroupService;
import com.cuhk.common.utils.PageUtils;
import com.cuhk.common.utils.R;



/**
 * 属性分组
 *
 * @author chenyuijing
 * @email torchsam@aliyun.com
 * @date 2024-06-10 12:02:34
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;

    /**
     * 新增关联关系
     * @return
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveRelationByBatch(vos);
        return R.ok();
    }

    /**
     * 查询分组关联的所有属性
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation (@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noAttrRelation (@PathVariable("attrGroupId") Long attrGroupId,
                             @RequestParam Map<String, Object> params) {
        PageUtils res = attrService.getRelationNoAttr(attrGroupId, params);
        return R.ok().put("page", res);
    }



    @PostMapping("/attr/relation/delete")
    public R deleteRelation (@RequestBody AttrGroupRelationVo[] vos) {
        relationService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCateLogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }


    /**
     * 查询catelogId下所有的属性分组
     * 查询结果还要携带每个分组下的，各个属性的实体
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> res = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", res);
    }

}
